//
//  VersionManager.m
//  SWCommonFrame
//
//  Created by shaowei on 13-6-18.
//  Copyright (c) 2013年 shaowei. All rights reserved.
//

#import "VersionManager.h"
#import "SVHTTPClient.h"

@implementation VersionManager

static VersionManager *sVersionManager;

- (void)dealloc
{
    [_updateUrl release];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    if (self) {
        _isNeedUpdate = NO;
    }
    return self;
}

#pragma mark - Function - Public

+ (VersionManager *)sharedInstance{
    if (!sVersionManager) {
        sVersionManager = [[VersionManager alloc] init];
    }
    return sVersionManager;
}

- (void)checkUpdate{
    _isNeedUpdate = [self needCheckUpdate];
    if (_isNeedUpdate) {
        //开始检测更新
    }
    
    
    //检测结果返回成功，则更新本地的更新记录
    [[NSUserDefaults standardUserDefaults] setDouble:[[NSDate date] timeIntervalSince1970]
                                              forKey:LAST_CHECK_DATE];
    
}



#pragma mark Function - Private

- (CGFloat)getAppVersionNumber{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
//    NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
//    NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    return [version floatValue];
}

- (BOOL)needCheckUpdate{
    NSTimeInterval lastCheckTime = [[NSUserDefaults standardUserDefaults] doubleForKey:LAST_CHECK_DATE];
    
    //检测本地是否有更新记录
    if ((lastCheckTime - 0) > 1) {
        NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970];
        NSTimeInterval intervalTime = CHECK_INTERVAL *24*60*60;
        BOOL isOverTime = (nowTime - lastCheckTime) > intervalTime;
        
        return isOverTime;
    }else{
        //还没有检测更新过
        return YES;
    }
    
}




@end
